/*
    Copyright 2010 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.util;

import java.util.Calendar;
import java.util.Date;
import java.text.*;

/**
 *
 * @author Barnet Wagman
 */
public class Time {

    public static long toTimeMillis(int year, int month, int dayInMonth,
                                    int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.setLenient(false);
        c.clear();
        c.set(year, month - 1, dayInMonth, hour, minute, second);
        return(c.getTimeInMillis());
    }

    public static String toTimeDate(long millis) {
        return(
          (new SimpleDateFormat("yyyy MMM d HH:mm:ss z")).format(
            new Date(millis))
        );        
    }

    public static String toTime(long millis) {
        return(
          (new SimpleDateFormat("HH:mm:ss z")).format(
            new Date(millis))
        );
    }

    public static boolean timedOut(long t0, long maxTime) {
        return( (System.currentTimeMillis() - t0) > maxTime );
    }

    public static String toDateTimeMilliKeyString(long tm) {
        return(
          (new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(
            new Date(tm))
        );
    }

    public static void nap(long millis) {
        try { Thread.sleep(millis); }
        catch(InterruptedException ix) {}
    }
}
